﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Model;
using NanoERP.Common;
using NanoERP.Business.BaseInfo;

namespace NanoERP.BaseInfo
{
    //[Module("FrmDepartStaff")]
    public partial class FrmDepartStaff : ModuleForm
    {
        Userinfo _userinfo;
        readonly UserinfoBLL _userinfoBll;
        readonly SysdictableBLL _sysdictablebLl;
        readonly Deptinfo _deptinfo;
        public FrmDepartStaff()
        {
            InitializeComponent();
        }

        public FrmDepartStaff(Deptinfo v)
        {
            InitializeComponent();

            _sysdictablebLl = new SysdictableBLL();
            _userinfoBll = new UserinfoBLL();
            _userinfo = new Userinfo();

            _deptinfo = v;

            FillFormInfo();
            BindFormContro();
        }

        public FrmDepartStaff(Userinfo userinfo, Deptinfo v)
        {
            InitializeComponent();

            _sysdictablebLl = new SysdictableBLL();
            _userinfoBll = new UserinfoBLL();
            _userinfo = userinfo;
            _deptinfo = v;

            FillFormInfo();
            BindFormContro();
        }

        void FillFormInfo()
        {
            cboGender.DataSource = EnumUtil.Enum2Table<GenderEnum>();
            cboPolitical.DataSource = EnumUtil.Enum2Table<PolitiEnum>();
            cboCulture.DataSource = EnumUtil.Enum2Table<CultureEnum>();
            cboMarriage.DataSource = EnumUtil.Enum2Table<MarriageEnum>();
            cboLogintype.DataSource = EnumUtil.Enum2Table<LoginTypeEnum>();
            cboJobrole.DataSource = _sysdictablebLl.Query((int)SysDicEnum.职务岗位);
        }

        /// <summary>
        /// 绑定控件值
        /// </summary>
        void BindFormContro()
        {
            txtDeptid.Text = _deptinfo.Deptname;
            _userinfo.Deptid = _deptinfo.Id;

            txtUsername.DataBindings.Add(new Binding("Text", _userinfo, "Username", true, DataSourceUpdateMode.OnPropertyChanged));
            txtUserpym.DataBindings.Add(new Binding("Text", _userinfo, "Userpym", true, DataSourceUpdateMode.OnPropertyChanged));
            txtUserno.DataBindings.Add(new Binding("Text", _userinfo, "Userno", true, DataSourceUpdateMode.OnPropertyChanged));
            cboGender.DataBindings.Add(new Binding("Text", _userinfo, "Gender", true, DataSourceUpdateMode.OnPropertyChanged));
            dtBirthday.DataBindings.Add(new Binding("ValueObject", _userinfo, "Birthday", true, DataSourceUpdateMode.OnPropertyChanged));
            dtFarmday.DataBindings.Add(new Binding("ValueObject", _userinfo, "Farmday", true, DataSourceUpdateMode.OnPropertyChanged));
            txtCardno.DataBindings.Add(new Binding("Text", _userinfo, "Cardno", true, DataSourceUpdateMode.OnPropertyChanged));
            txtNationstate.DataBindings.Add(new Binding("Text", _userinfo, "Nationstate", true, DataSourceUpdateMode.OnPropertyChanged));
            txtDeptid.DataBindings.Add(new Binding("Tag", _userinfo, "Deptid", true, DataSourceUpdateMode.OnPropertyChanged));
            cboJobrole.DataBindings.Add(new Binding("Text", _userinfo, "Jobrole", true, DataSourceUpdateMode.OnPropertyChanged));
            txtMobilephone.DataBindings.Add(new Binding("Text", _userinfo, "Mobilephone", true, DataSourceUpdateMode.OnPropertyChanged));
            txtHomephone.DataBindings.Add(new Binding("Text", _userinfo, "Homephone", true, DataSourceUpdateMode.OnPropertyChanged));
            txtOtherphone.DataBindings.Add(new Binding("Text", _userinfo, "Otherphone", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPostcode.DataBindings.Add(new Binding("Text", _userinfo, "Postcode", true, DataSourceUpdateMode.OnPropertyChanged));
            txtAbode.DataBindings.Add(new Binding("Text", _userinfo, "Abode", true, DataSourceUpdateMode.OnPropertyChanged));
            txtAddress.DataBindings.Add(new Binding("Text", _userinfo, "Address", true, DataSourceUpdateMode.OnPropertyChanged));
            cboLogintype.DataBindings.Add(new Binding("Text", _userinfo, "Logintype", true, DataSourceUpdateMode.OnPropertyChanged));
            cboCulture.DataBindings.Add(new Binding("Text", _userinfo, "Culture", true, DataSourceUpdateMode.OnPropertyChanged));
            dtComeindate.DataBindings.Add(new Binding("ValueObject", _userinfo, "Comeindate", true, DataSourceUpdateMode.OnPropertyChanged));
            cboMarriage.DataBindings.Add(new Binding("Text", _userinfo, "Marriage", true, DataSourceUpdateMode.OnPropertyChanged));
            txtFinishschool.DataBindings.Add(new Binding("Text", _userinfo, "Finishschool", true, DataSourceUpdateMode.OnPropertyChanged));
            txtNote.DataBindings.Clear();
            txtNote.DataBindings.Add(new Binding("Text", _userinfo, "Note", true, DataSourceUpdateMode.OnPropertyChanged));

            chIsvalid.Checked = _userinfo.Isvalid.Value.Equals(1);
            chCanlogin.Checked = _userinfo.Canlogin.Value.Equals(1);
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            _userinfo = new Userinfo();
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            SaveInfo();
        }

        private void SaveInfo(bool isAdd = true)
        {
            if (txtUsername.Text.Length == 0)
            {
                MessageBox.Show("姓名不能为空，请输入.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUsername.Focus();
                return;
            }

            _userinfo.Isvalid = chIsvalid.Checked ? (short)1 : (short)0;
            _userinfo.Canlogin = chCanlogin.Checked ? (short)1 : (short)0;

            _userinfoBll.Edit(_userinfo);

            MessageBox.Show("保存成功.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (isAdd) this.DialogResult = DialogResult.OK;
        }

        private void tsbSaveAdd_Click(object sender, EventArgs e)
        {
            SaveInfo(false);
            _userinfo = new Userinfo();
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();
        }

        private void tsmPre_Click(object sender, EventArgs e)
        {
            PreviewRecord(_userinfo.Id - 1, 0);
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            PreviewRecord(_userinfo.Id + 1, 1);
        }

        /// <summary>
        /// 浏览记录 上一条 下一条
        /// </summary>
        /// <param name="id">浏览记录Id</param>
        /// <param name="type">类型 0-向上,1-向下</param>
        private void PreviewRecord(long id, int type)
        {
            bool isLast = false;
            var obj = _userinfoBll.PreRecord(id, type, ref isLast);
            _userinfo = obj;
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();

            if (isLast) MessageBox.Show(string.Format("当前已是{0}一条记录", type.Equals(0) ? "第" : "最后"));
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtUsername_Leave(object sender, EventArgs e)
        {
            txtUserpym.Text = ChineseToSpell.GetChineseSpell(txtUsername.Text);
        }

        private void btnDeptid_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDepartForm(txtDeptid);
            _userinfo.Deptid = Normal.ToInt(txtDeptid.Tag);
        }

        private void btnJobrole_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboJobrole, SysDicEnum.职务岗位);
        }
    }
}
